<?php
require_once 'Zend/Gdata/YouTube.php';

/**
 * AMF Webservice class used to communicate with the YouTube API
 * @author Cristina Costache
 */
class Youtube
{
	public function searchVideo($searchTerm, $startIndex, $maxResults)
	{
		$yt = new Zend_Gdata_YouTube();
		$query = $yt->newVideoQuery();
		$query->setQuery($searchTerm);
		$query->setStartIndex($startIndex);
		$query->setMaxResults($maxResults);

		/* check for one of the standard feeds, or list from 'all' videos */
		$queryType = 'all';
		switch ($queryType) {
			case 'most_viewed':
			$query->setFeedType('most viewed');
			$query->setTime('this_week');
			$feed = $yt->getVideoFeed($query);
			break;
			case 'most_recent':
			$query->setFeedType('most recent');
			$feed = $yt->getVideoFeed($query);
			break;
			case 'recently_featured':
			$query->setFeedType('recently featured');
			$feed = $yt->getVideoFeed($query);
			break;
			case 'top_rated':
			$query->setFeedType('top rated');
			$query->setTime('this_week');
			$feed = $yt->getVideoFeed($query);
			break;
			case 'all':
			$feed = $yt->getVideoFeed($query);
			break;
			default:
			return 'ERROR - unknown queryType - "' . $queryType . '"';
			break;
		}

		$results = array();

		foreach ($feed as $entry) {
			$result = array();
			$result['videoId'     ] = $entry->getVideoId();
			$result['thumbnailUrl'] = (string)$entry->mediaGroup->thumbnail[0]->url;
			$result['title'       ] = (string)$entry->mediaGroup->title;
			$result['description' ] = (string)$entry->mediaGroup->description;
			$results[] = $result;
		}

		return $results;
	}
}